const { SysUser, sequelize, SysDept } = require('../config/Sequelize')
const { CriteriaExample } = require('../utils/CriteriaExample')

/**
 * 查询用户列表
 * @param {*} user 
 * @returns 
 */
const selectUserList = async (user = {}) => {
  const userExample = new CriteriaExample(user)
  userExample.and()
    .eq('userId', user.userId)
    .like('userName', user.userName)
    .like('loginName',user.loginName)
    .eq('status', user.status)
    .between('createdAt',user.createdAt)
    .eq('delFlag', user.delFlag)
  console.log(userExample.getWhere())
  const deptExample = new CriteriaExample(user)
  const userList = await SysUser.findAll({
    where: userExample.getWhere(),
    include: [{
      model: SysDept, required: false,
      where: deptExample.getWhere()
    }],
  })
  return userList
}

/**
 * 根据用户id查询用户信息
 * @param {*} id 
 * @returns 
 */
const selectUserById = async (id) => {
  console.log(id)
  const user = await SysUser.findOne({
    where: {
      user_id: id
    },
    include: [{
      model: SysDept, required: false,
    }]
  })
  return user
}


/**
 * 添加用户信息
 * @param {*} user 
 * @returns 
 */
const addUser = async (user) => {
  return SysUser.create(user)
}

/**
 * 更新用户信息
 * @param {*} user 
 * @returns 
 */
const updateUser = async (user) => {
  return SysUser.update(user, {
    where: {
      userId: user.userId
    }
  })
}

/**
 * 删除用户信息
 * @param {*} user 
 * @returns 
 */
const deleteUser = async (id) => {
  return SysUser.destroy({
    where: { userId: id }
  })
}

module.exports = {
  selectUserList,
  addUser,
  updateUser,
  deleteUser,
  selectUserById
}